<html>
	<head>
	<script src="webScrapper.js"></script>
	<script src="jquery-1.6.2.min.js"></script>
	<script src="getFilesList.js"></script>
	<script src="testFileTwo.js"></script>
	</head>
	<body onload="main()">
	</body>

<script type="text/javascript"> 
var count = 0;
main = function()
	{
	//donedonedoneagaing
	main.pateintArr = new Object();
	//main.filesArr
	var REF_TAB1 = 'http://tcga-data.nci.nih.gov/tcgafiles/ftp_auth/distro_ftpusers/anonymous/tumor/ov/bcr/minbiotab/clin/clinical_patient_public_ov.txt';
	main.FILE_IND = 'http://tcga-data.nci.nih.gov/tcgafiles/ftp_auth/distro_ftpusers/anonymous/tumor/ov/cgcc/broad.mit.edu/ht_hg-u133a/transcriptome/';
	getFilesList(main.FILE_IND);
	webScrapper.getURL(REF_TAB1, saveTable1);
	return false;
	}

saveTable1 = function(pageObject)
	{
	var REF_TAB2 = 'http://tcga-data.nci.nih.gov/tcgafiles/ftp_auth/distro_ftpusers/anonymous/tumor/ov/cgcc/broad.mit.edu/ht_hg-u133a/transcriptome/broad.mit.edu_OV.HT_HG-U133A.mage-tab.1.1007.0/broad.mit.edu_OV.HT_HG-U133A.sdrf.txt';
	
	var header = pageObject.content.shift();
	header=header.split('\t');
	for( var i in pageObject.content )
		{
		if( !pageObject.content[i] ){continue;}
		var lineArr = pageObject.content[i].split('\t');
		main.pateintArr[lineArr[0]]=new Object();
		main.pateintArr[lineArr[0]].pateintID=lineArr[0];
		main.pateintArr[lineArr[0]].clinicalInfo = new Object();
		for( var j in lineArr )
			{
			main.pateintArr[lineArr[0]].clinicalInfo[header[j]]=lineArr[j];
			}
		}
	//console.log(main.pateintArr);
	webScrapper.getURL(REF_TAB2, saveTable2);
	//saveTable2( dummyVar );
	return false;
	}

saveTable2 = function(pageObject)
	{
	var header = pageObject.content.shift();
	header=header.split('\t');
	for( var i in pageObject.content )
		{
		if( !pageObject.content[i] ){continue;}
		var lineArr = pageObject.content[i].split('\t');
		var pateintID = lineArr[0].slice(0,12);
		main.addData( pateintID, lineArr, header );
		}
	return false;
	}

main.addData = function( patID, line, header )
	{
	if(count>2) {return false; };
	
	if( main.pateintArr[patID] == null )
		{
		main.pateintArr[patID]=new Object;
		}
	main.pateintArr[patID].refData = new Object;
	for( var j in line )
		{
		if( line[j] == '->' ) { line[j] = 'undefined'; }
		main.pateintArr[patID].refData[header[j]]=line[j];
		}
	
	}
	
	
main.addExpressionFileLocation = function( )
	{
	
	for( var patID in main.pateintArr )
		{
		var FILE_KEY = 'Derived Array Data Matrix File';
		if( main.pateintArr[patID].refData )
			{
			var filename = main.pateintArr[patID].refData[FILE_KEY];
			if( filename != undefined && getFilesList[main.FILE_IND][filename] )
				{
				main.pateintArr[patID].expressionFileLocation =getFilesList[main.FILE_IND][filename]; 
				}
			}
		}
	
	}
main.addExpressionData = function(patID)
	{
	if( main.pateintArr[patID].expressionFileLocation )
		{
		if( !main.pateintArr[patID].expressionData ) { main.pateintArr[patID].expressionData = new Object(); }
		webScrapper.getURL( main.pateintArr[patID].expressionFileLocation, main.addExpressionData.addData, patID );
		}
	else
		{
		console.log( "No file found for: " + patID + "\n" );
		}
	}
main.addExpressionData.addData = function( pageObject, patID )
	{
	pageObject.content.shift();
	var header = pageObject.content.shift();
	for( var i in pageObject.content )
		{
		if( !pageObject.content[i] ) {continue;}
		var line = pageObject.content[i].split('\t');
		main.pateintArr[patID].expressionData[line[0]]=line[1];
		}
	console.log(main.pateintArr[patID]);
	}
	
</script>
</html>